package org.nmgyjt.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.nmgyjt.springboot.entity.rescueTeam.RescueTeam;
import org.nmgyjt.springboot.entity.storeInfo.StoreInfo;

import java.util.List;
import java.util.Map;

@Mapper
public interface StoreMapper extends BaseMapper<StoreInfo> {
    @Select("SELECT a.* FROM (SELECT\n" +
            "\tDISTINCT a.org_name,\n" +
            "\ta.`name`,\n" +
            "\ta.phone,\n" +
            "\tr.address,\n" +
            "\tr.protect_num,\n" +
            "\tr.flood_num,\n" +
            "\tr.shockproof_num,\n" +
            "\tr.disaster_num,\n" +
            "\tr.other_num,\n" +
            "\tr.dep_id,\n" +
            "\tr.state,\n" +
            "\ta.org_id\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
//            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN store_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000011'   ${sql}   ) a " +
            "WHERE a.org_id is not null ${sql2}")
    List<Map<String,Object>> statisticStore(String sql, String sql2);

    @Select("SELECT a.* FROM (SELECT\n" +
            "\t DISTINCT a.org_name,\n" +
            "\ta.`name`,\n" +
            "\ta.phone,\n" +
            "\tr.address,\n" +
            "\tr.protect_num,\n" +
            "\tr.flood_num,\n" +
            "\tr.shockproof_num,\n" +
            "\tr.disaster_num,\n" +
            "\tr.other_num,\n" +
            "\tr.dep_id,\n" +
            "\ta.org_id\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
//            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN store_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000011' " +
            " AND a.superior_org=#{createdBy, jdbcType=VARCHAR}" +
            "  AND r.state <> '1' ) a")
    List<Map<String,Object>> statisticStoreCount(@Param("createdBy") String createdBy);

    @Select("SELECT COUNT(1) FROM (SELECT\n" +
            "\tDISTINCT a.org_name,\n" +
            "\ta.`name`,\n" +
            "\ta.phone,\n" +
            "\tr.address,\n" +
            "\tr.protect_num,\n" +
            "\tr.flood_num,\n" +
            "\tr.shockproof_num,\n" +
            "\tr.disaster_num,\n" +
            "\tr.other_num,\n" +
            "\tr.dep_id,\n" +
            "\ta.org_id\n" +
            "FROM\n" +
            "\tt_cs_system_admin a\n" +
//            "\tRIGHT JOIN t_cs_system_source_ext ext ON a.org_id = ext.org_id\n" +
            "\tLEFT JOIN store_info r ON a.org_id = r.dep_id\n" +
            "WHERE\n" +
            "\ta.menu_id='1000000011' " +
            " AND a.superior_org=#{createdBy, jdbcType=VARCHAR}" +
            "  AND r.state = '0' ) a")
    int statisticStoreCountStatus(@Param("createdBy") String createdBy);

    @Update("UPDATE store_info a SET a.state '1' WHERE a.dep_id = #{depId,jdbcType=VARCHAR}")
    Integer deleteByDepId(@Param("depId") String depId);
}
